C#_C#、ASP.NET通用扩展工具类之LogicSugar,说明一下性能方面 还可以接受
手痛吗}//现在写法myBook =bookKey.Switch().Case("c#", T falseValue){if (thisValue == true){return trueValue;}else{return falseValue;}}/// summary/// 所有值为true。
false返回string.Empty;/// /summary/// typeparam/typeparam/// param/param/// param值为true返回 trueValue/param/// returns/returnspublic static string IIF(this bool thisValue, T caseValue,小项目也不会计较这点性能了, 注意需要引用 “SyntacticSugar” 用法: //【Switch】string bookKey = "c#";//以前写法string myBook = "";switch (bookKey){ case "c#":myBook = "asp.net技术";break; case "java":myBook = "java技术";break; case "sql":myBook = "mssql技术";break; default:myBook = "要饭技术";break;//打这么多break和封号, 2);string id = "";string id2 = "";//以前写法isSuccess = (id == id2) (id != null Convert.ToInt32(id) 0);//现在写法isSuccess = (id == id2).And(id != null,则返回true否则返回false/// /summary/// param/param/// param/param/// returns/returnspublic static bool Or(this bool thisValue,/// /summary/// typeparam/typeparam/// param/param/// param值为true返回 trueValue/param/// param值为false返回 falseValue/param/// returns/returnspublic static T IIFT(this bool thisValue, T trueValue,false返回0/// /summary/// typeparam/typeparam/// param/param/// param值为true返回 trueValue/param/// returns/returnspublic static int IIF(this bool thisValue,*/bool isSuccess = true;//【IIF】//以前写法var trueVal1 = isSuccess 100 :0;//现在写法var trueVal2 = isSuccess.IIF(100);//以前写法var str = isSuccess "我是true" : "";//现在写法var str2 = isSuccess.IIF("我是true");//以前写法var trueVal3 = isSuccess 1 : 2;//现在写法var trueVal4 = isSuccess.IIF(1, int trueValue){if (thisValue){return trueValue;}else{return 0;}}/// summary/// 根据表达式的值,使用自定义扩展函数就没有这种问题了, "sql技术").Default("要饭技术").Break();//点的爽啊/**C#类里看不出效果, string trueValue){if (thisValue){return trueValue;}else{return string.Empty;}}/// summary/// 根据表达式的值,嵌套多了很不美观, "java技术").Case("sql", Convert.ToInt32(id) 0);//以前写法isSuccess = id != null || id != id2;//现在写法isSuccess = (id != null).Or(id != id2); 源码: using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace SyntacticSugar{ /// summary /// ** 描述:逻辑糖来简化你的代码 /// ** 创始时间:2015-6-1 /// ** 修改时间:- /// ** 作者:sunkaixuan /// /summary public static class LogicSugarExtenions {/// summary/// 根据表达式的值,扩展函数为0.002秒 ,来返回两部分中的其中一个, TReturn(this SwitchSugarModelT switchSugar,则返回true否则返回false/// /summary/// param/param/// param/param/// returns/returnspublic static bool And(this bool thisValue。
需要外面加一个括号,节约性能return reval;}public class SwitchSugarModelT{public T SourceValue { get; set; }public bool IsEquals { get; set; }public dynamic ReturnVal { get; set; }}}} 。
params bool[] andValues){return thisValue || andValues.Where(c = c == true).Any();}/// summary/// Switch().Case().Default().Break()/// /summary/// typeparam/typeparam/// param/param/// returns/returnspublic static SwitchSugarModelT SwitchT(this T thisValue){var reval = new SwitchSugarModelT();reval.SourceValue = thisValue;return reval;}public static SwitchSugarModelT CaseT,来返回两部分中的其中一个,true返回trueValue, TReturn returnValue){if (switchSugar.IsEquals == false)switchSugar.ReturnVal = returnValue;return switchSugar;}public static dynamic BreakT(this SwitchSugarModelT switchSugar){string reval = switchSugar.ReturnVal;switchSugar = null;//清空对象, 在mvc razor里 、 或者 || 直接使用都会报错, TReturn(this SwitchSugarModelT switchSugar, "asp.net技术").Case("java", 说明一下性能方面 还可以接受 循环1000次普通Switch是用了0.001秒 , TReturn returnValue){if (switchSugar.SourceValue.Equals(caseValue)){switchSugar.IsEquals = true;switchSugar.ReturnVal = returnValue;}return switchSugar;}public static SwitchSugarModelT DefaultT,/// /summary/// typeparam/typeparam/// param/param/// param值为true返回 trueValue/param/// param值为false返回 falseValue/param/// returns/returnspublic static T IIFT(this bool thisValue,true返回trueValue, T falseValue){if (thisValue){return trueValue;}else{return falseValue;}}/// summary/// 根据表达式的值, T trueValue, params bool[] andValues){return thisValue !andValues.Where(c = c == false).Any();}/// summary/// 只要有一个值为true, 如果是大项目在有负载均衡的情况下完全可以无视掉,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/c/11741.shtml
